<template>
  <div class="header">
    <div class="header-top">
      <span v-if="name"
        >尚品汇欢迎{{ name
        }}<button @click="handleClick">退出登录</button></span
      >
      <span v-if="!name"
        >尚品汇欢迎您！请&nbsp;<router-link to="/login">登录</router-link
        ><router-link to="/register" class="header-top-register"
          >免费注册</router-link
        >
      </span>
      <ul class="header-top-link">
        <li>
          <router-link to="/center">我的订单</router-link>
        </li>
        <li>
          <router-link to="/shopcart">我的购物车</router-link>
        </li>
        <li>
          <router-link to="/">我的尚品汇</router-link>
        </li>
        <li>
          <router-link to="/">尚品汇会员</router-link>
        </li>
        <li>
          <router-link to="/">企业采购</router-link>
        </li>
        <li>
          <router-link to="/">关注尚品汇</router-link>
        </li>
        <li>
          <router-link to="/">合作招商</router-link>
        </li>
        <li>
          <router-link to="/">商家后台</router-link>
        </li>
      </ul>
    </div>
    <div class="header-logo">
      <router-link to="/">
        <img src="./logo.png" alt="logo" />
      </router-link>
      <form class="header-logo-search" @submit.prevent="toSearch">
        <input type="text" v-model="keyword" />
        <button>搜索</button>
      </form>
    </div>
  </div>
</template>

<script>
import { mapState, mapActions } from "vuex";

export default {
  name: "Header",
  data() {
    return {
      keyword: "",
    };
  },
  methods: {
    ...mapActions("user", ["logout"]),
    toSearch() {
      const keyword = this.keyword.trim();
      const location = {
        name: "Search",
        query: this.$route.query,
      };
      if (keyword) {
        location.params = { keyword };
      }
      this.$router.history.push(location);
    },
    async handleClick() {
      await this.logout();
      //当退出登录之后要把localStorage和sessionStorage中的user数据清除掉
      localStorage.removeItem("user");
      sessionStorage.removeItem("user");
    },
  },
  computed: {
    ...mapState("user", ["name"]),
  },
};
</script>

<style lang="less" scoped>
.header {
  width: 100%;
  min-width: 1200px;
}
.header-top {
  display: flex;
  justify-content: space-between;
  background-color: #f4f4f4;
  padding: 10px 20px;
}
.header-top-register {
  margin-left: 5px;
  padding-left: 5px;
  border-left: 1px solid #333;
}
.header-top-link {
  display: flex;
  li {
    margin-right: 10px;
    padding-right: 10px;
    border-right: 1px solid #333;
    &:last-child {
      margin: 0;
      padding: 0;
      border: none;
    }
  }
}
.header-logo {
  display: flex;
  justify-content: space-between;
  padding: 0 20px;
}
.header-logo-search {
  display: flex;
  align-items: center;
  input {
    outline: none;
    height: 30px;
    font-size: 16px;
    width: 450px;
    border: 1px solid #ea4a36;
    padding: 0 5px;
    box-sizing: border-box;
  }
  button {
    width: 80px;
    height: 30px;
    background-color: #ea4a36;
    border: 1px solid #ea4a36;
    font-size: 14px;
    color: #fff;
  }
}
</style>